package org.geeks.controller;

import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.geeks.domain.User;
import org.geeks.repository.UserRepository;
import org.geeks.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户Controller
 * 
 * @author Geek4J
 *
 */
@RequestMapping("user")
@Controller
public class UserController {

	@Autowired
	private UserService userService;

	@Autowired
	private UserRepository userRepository;

	/**
	 * 查询所有用户
	 * 
	 * @return List<User> 用户列表
	 */
	@GetMapping("findAll")
	@ResponseBody
	public List<User> findAll() {
		return userService.findAll();
	}

	/**
	 * 转发到Freemarker／Thymeleaf
	 * 
	 * @return String viewName
	 */
	@GetMapping("{id}.html")
	public String findOne(@PathVariable Long id, String template, Model model, HttpSession session) {
		User user = userRepository.findOne(id);
		model.addAttribute("user", user);
		model.addAttribute("options", Arrays.asList("Option-0", "Option-1", "Option-2", "Option-3"));
		session.setAttribute("username", "oo");
		return template;
	}
}
